#include "sem_demo.h"
UINT16 semCount = 2;
extern TSK_INIT_PARAM_S task1;
extern TSK_INIT_PARAM_S task2;

static VOID TaskF01(void)
{
    UINT32 ret;
    printf("\n++++++++Yang Zihao's TaskF01: (1) before sem pend success, check actual Count = %d, it should be 2\n", Count);
    ret = LOS_SemPend(g_semTest1, LOS_WAIT_FOREVER);
    Count++;
    // LOS_TaskDelay(50);
    ret = LOS_SemPost(g_semTest1);
    printf("\n++++++++Yang Zihao's TaskF01: (2) before sem post success, check actual Count = %d, it should be 3\n", Count);
    return;
}

static VOID TaskF02(void)
{
    UINT32 ret;
    Count++;
    printf("\n++++++++Yang Zihao's TaskF02: (1) before sem pend check actual Count = %d, it should be 1\n", Count);
    ret = LOS_SemPend(g_semTest1, LOS_WAIT_FOREVER);
    Count++;
    printf("\n++++++++Yang Zihao's TaskF02: (2) after sem pend (before sem post) check actual Count = %d, it should be 2\n", Count);
    printf("\n++++++++Yang Zihao's TaskF02: (3) continue hold sem for 50 ticks, TaskF01 should be scheduled\n");
    LOS_TaskDelay(50);
    printf("\n++++++++Yang Zihao's TaskF02: (4) sem post, TaskF01 will continue exe within the next 50 ticks\n");
    ret = LOS_SemPost(g_semTest1);
    LOS_TaskDelay(10);
    Count++;
    printf("\n++++++++Yang Zihao's TaskF02: (5) after sem post check actual Count = %d, it should be 4\n", Count);
}

VOID  Sem_Demo(VOID)
{
    UINT32 ret;
    ret = LOS_SemCreate(semCount,&g_semTest1);
    if(ret == LOS_OK)
        printf("\n++++++++Yang Zihao's Sem Demo: sem created ok with handle = 0x%x\n", Count);
    
    LOS_TaskLock();
    task1.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF01;
    task1.usTaskPrio = TASK_PRIO_TEST;
    task1.pcName = "VSemB2_1";
    task1.uwStackSize = 0x900;
    task1.uwResved = 0;

    ret = LOS_TaskCreate(&g_testTaskID01, &task1);
    if(ret == LOS_OK)
        printf("\n++++++++Yang Zihao's Sem Demo: TaskF01 created ok with ID = 0x%x\n", g_testTaskID01);

    task2.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF02;
    task2.usTaskPrio = (TASK_PRIO_TEST - 2); 
    task2.pcName = "VSemB2_2";
    task2.uwStackSize = 0x900;
    task2.uwResved = 0;

    ret = LOS_TaskCreate(&g_testTaskID02, &task2);
    if(ret == LOS_OK)
        printf("\n++++++++Yang Zihao's Sem Demo: TaskF02 created ok with ID = 0x%x\n", g_testTaskID02);

    LOS_TaskUnlock();
    printf("\n++++++++Yang Zihao's Sem Demo: will delay 90 ticks\n");
    LOS_TaskDelay(90);

    printf("\n++++++++Yang Zihao's Sem Demo: after 90 ticks, finally Count = %d\n", Count);

    ret = LOS_SemDelete(g_semTest1);
    if(ret == LOS_OK)
        printf("\n++++++++Yang Zihao's Sem Demo: g_semTest1 deleted ok\n");

    return; 
}


